﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Yipusoft.Common.BLL;
using Yipusoft.Exercitation.BLL;
using Yipusoft.Exercitation.Model;
using Yipusoft.Exercitation.DBUtility;
using Yipusoft.SoldierArchives.Model;
using System.Data;

public partial class Combat_SoldierScoreStat : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            InitData();
            ListPageContral1.SetFirstPage();
            Bind(0);
        }
    }  

    private void InitData()
    { 
        for (int i = 2010; i <= 2050; i++)
        {
            ddlYear.Items.Add(new ListItem(i.ToString() + "年", i.ToString()));
        }
        for (int i = 1; i <= 12; i++)
        {
            ddlMonth.Items.Add(new ListItem(i.ToString() + "月", i.ToString()));
        }

        ddlYear.SelectedValue = DateTime.Now.Year.ToString();
        ddlMonth.SelectedValue = DateTime.Now.Month.ToString();
        ddlDepartment.DataSource= Yipusoft.Account.BLL.Department.GetList("Right(DepartmentID,3)='000' And Left(DepartmentID,4)<>'1314' And Left(DepartmentID,4)<>'1315' And DepartmentID<>'1300000'");
        ddlDepartment.DataTextField = "FullName";
        ddlDepartment.DataValueField = "DepartmentID";
        ddlDepartment.DataBind();

        ddlDepartment2.DataSource = Yipusoft.Account.BLL.Department.GetList(" DepartmentID IN (Select DepartmentID From TaskSquadron Where TaskID='" + GetTaskID() + "' And Left(DepartmentID,4)='" + ddlDepartment.SelectedValue.Substring(0, 4) + "') Order by DepartmentID");
        ddlDepartment2.DataTextField = "FullName";
        ddlDepartment2.DataValueField = "DepartmentID";
        ddlDepartment2.DataBind();
       

        ddlStation.DataSource = CodeItem.GetList(" ParentID='SBGW0000'");
        ddlStation.DataTextField = "itemName";
        ddlStation.DataValueField = "ItemID";
        ddlStation.DataBind();
        ddlStation.Items.Insert(0, new ListItem("全部", "0000"));

        ddlStation.SelectedValue = string.IsNullOrEmpty(Request["StationID"]) ? "0000" : Request["StationID"];

        ddlRank.DataSource = CodeItem.GetList(" ParentID='ZXSBJX00'");
        ddlRank.DataTextField = "ItemName";
        ddlRank.DataValueField = "ItemID";
        ddlRank.DataBind();
        ddlRank.Items.Insert(0, new ListItem("全部", "0000"));

        ddlSkill.DataSource= TaskSkill.GetList(" SkillID IN (Select SkillID From TaskSubject Where TaskID='" + GetTaskID() + "' Group by SkillID)");
        ddlSkill.DataTextField = "SkillName";
        ddlSkill.DataValueField = "SkillID";
        ddlSkill.DataBind();

    }

    public void ListPageContral1_PageSizeChange(int pageSize, int PageIndex)
    {
        Bind(PageIndex);
    }
    public string GetTaskID()
    {
        TaskBaseInfo taskInfo = TaskBase.GetList(" Year(EndDate)=" + ddlYear.SelectedValue + " And Month(EndDate)=" + ddlMonth.SelectedValue + " And IsOn='1'").FirstOrDefault();
        return taskInfo != null ? taskInfo.TaskID.ToString() : Guid.Empty.ToString();
    }
    public string GetRankAndStation()
    {
        string where = string.Empty;
        if (!ddlRank.SelectedValue.Equals("0000"))
        {
            where = " And RankID='" + ddlRank.SelectedValue + "'";
        }
        if (!ddlStation.SelectedValue.Equals("0000"))
        {
            where += " And StationID='" + ddlStation.SelectedValue + "'";
        }

        return where;
    }
    private void Bind(int pageIndex)
    {
        //DataTable dtb = new DataTable();
        //DataColumn clm;
        //DataRow drw;
        //clm = new DataColumn("SoldierID", typeof(System.String));
        //dtb.Columns.Add(clm);
        //clm = new DataColumn("SoldierName", typeof(System.String));
        //dtb.Columns.Add(clm);
        //clm = new DataColumn("InputScore", typeof(System.Decimal)); //录入率
        //dtb.Columns.Add(clm);
        //clm = new DataColumn("OrderScore", typeof(System.String)); //录入率
        //dtb.Columns.Add(clm);

        Yipusoft.SoldierArchives.BLL.Soldier soldier = new Yipusoft.SoldierArchives.BLL.Soldier();
        string where = string.Empty;
        string where2 = string.Empty;
        if (!string.IsNullOrEmpty(txtSoldierName.Text.Trim()))
            where = " And SoldierName like '%" + txtSoldierName.Text + "%'";

	string skillID=ddlSkill.SelectedValue==""?Guid.Empty.ToString():ddlSkill.SelectedValue;
        where2 = " IsContract='" + rbtnContract.SelectedValue + "' And SoldierID IN( Select SoldierID From FireMis..TaskSoldiers Where DepartmentID='" + ddlDepartment2.SelectedValue + "' " + GetRankAndStation() + " And TaskID='" + GetTaskID() + "') And SoldierID IN (Select SoldierID From FireMis..TaskScoreView Where DepartmentID='" + ddlDepartment2.SelectedValue + "' And TaskID='" + GetTaskID() + "' And SkillID='" + skillID + "' And IsPass='" + rbtnPass.SelectedValue + "')";

        where2 += where;
	//Response.Write(where2);
        List<Yipusoft.SoldierArchives.Model.SoldierArchiveInfo> soldierList = soldier.GetSoldiersByWhere(where2, "SoldierID", "Desc", ListPageContral1.PageSize, pageIndex);
        ListPageContral1.ItemCount = soldier.GetSoldiersCountByWhere(where2);


        UserGrid.DataSource = soldierList;
        UserGrid.DataBind();
//Response.Write(where2 );
        //foreach (SoldierArchiveInfo info in soldierList)
        //{
        //    drw = dtb.NewRow();
        //    drw["SoldierID"] = info.SoldierID;
        //    drw["SoldierName"] = info.SoldierName;
        //    object[] array = GetScore(info.DepartmentID, info.SoldierID);
        //    drw["InputScore"] = Math.Round((decimal)array[0], 2);
        //    drw["OrderScore"] = array[1].ToString();
        //    dtb.Rows.Add(drw);

        //}
    }
    public object[] GetScore(string DepartmentID, string soldierID)
    {
        object[] array = new object[2];
        string itemWhere = string.Empty;
        if (!ddlSkill.SelectedValue.Equals("0000"))
            itemWhere = " And SkillID = '"+ddlSkill.SelectedValue+"'";

        string itemWhere2 = string.Empty;
        //if(!string.IsNullOrEmpty(GetRankAndStation()))
        //{
        //    itemWhere2 = " And SoldierID IN (Select SoldierID From TaskSoldiers Where 1=1 " + GetRankAndStation() + "  And DepartmentID='" + DepartmentID + "')";
        //}

        //if (!string.IsNullOrEmpty(txtSolderName.Text.Trim()))
        //{
        string whereSoldier = " And SoldierID='" + soldierID + "'";
        //}

        //已录入且已达标的技能
        string sql = "Select Count(*) From TaskScoreView Where IsPass=1 And TaskID='" + GetTaskID() + "' And DepartmentID='" + DepartmentID + "'" + itemWhere + whereSoldier;
        decimal count = decimal.Parse(SqlHelper.ExecuteScalar(SqlHelper.CONN_STRING, CommandType.Text, sql, null).ToString());

        //应录入的技能数量
        sql = "Select Count(*) From ( Select b.ID,a.DepartmentID,a.SoldierID,a.TaskID,a.RankID,a.StationID From TaskSoldiersView a  join TaskItems  b on a.RankID=b.RankID And a.StationID=b.StationID and a.TaskID=b.TaskID"
            + " And a.TaskID='" + GetTaskID() + "' ) as c  "
            + " join TaskSubject d on c.ID=d.TaskItemID  And  d.TaskID='" + GetTaskID() + "' And DepartmentID= '" + DepartmentID + "'" + itemWhere + whereSoldier;
        decimal count2 = decimal.Parse(SqlHelper.ExecuteScalar(SqlHelper.CONN_STRING, CommandType.Text, sql, null).ToString());

        if (count2 > 0)
            array[0] = (count / count2) * 100;
        else
            array[0] = 0;

        array[1] = "合格技能:" + count.ToString() + " 总技能:" + count2.ToString();

        return array;

    }
    protected void btnQuery_Click(object sender, EventArgs e)
    {
        ListPageContral1.SetFirstPage();
        Bind(0);
    }
    protected void ddlDepartment_SelectedIndexChanged(object sender, EventArgs e)
    {
       ddlDepartment2.DataSource= Yipusoft.Account.BLL.Department.GetList(" DepartmentID IN (Select DepartmentID From TaskSquadron Where TaskID='" + GetTaskID() + "' And Left(DepartmentID,4)='" + ddlDepartment.SelectedValue.Substring(0,4) + "') Order by DepartmentID");
       ddlDepartment2.DataTextField = "FullName";
       ddlDepartment2.DataValueField = "DepartmentID";
       ddlDepartment2.DataBind();
    }
    protected void UserGrid_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label ID = (Label)e.Row.FindControl("ID");
            ID.Text = (e.Row.RowIndex + 1).ToString();
            //当鼠标放上去的时候 先保存当前行的背景颜色 并给附一颜色 
            e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#F5F5F5',this.style.fontWeight='';");
            //当鼠标离开的时候 将背景颜色还原的以前的颜色 
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor,this.style.fontWeight='';");
        }
    }
    protected void ddlRank_SelectedIndexChanged(object sender, EventArgs e)
    {
	//Response.Write(GetTaskID());
        ddlSkill.DataSource = TaskSkill.GetList(" SkillID IN (Select SkillID From TaskSubject Where TaskID='" + GetTaskID() + "' And TaskItemID IN (Select ID From TaskItems Where 1=1 " + GetRankAndStation() + " And TaskID='" + GetTaskID() + "' ))");
        ddlSkill.DataTextField = "SkillName";
        ddlSkill.DataValueField = "SkillID";
        ddlSkill.DataBind();
    }
    protected void ddlStation_SelectedIndexChanged(object sender, EventArgs e)
    {
        ddlSkill.DataSource = TaskSkill.GetList(" SkillID IN (Select SkillID From TaskSubject Where TaskID='" + GetTaskID() + "' And TaskItemID IN (Select ID From TaskItems Where 1=1 " + GetRankAndStation() + " And TaskID='" + GetTaskID() + "' ))");
        ddlSkill.DataTextField = "SkillName";
        ddlSkill.DataValueField = "SkillID";
        ddlSkill.DataBind();
    }

    public string GetScore(string soldierID)
    {
        TaskScoreInfo scoreInfo= TaskScore.GetList(" TaskID='" + GetTaskID() + "' And SoldierID='" + soldierID + "' And SkillID='" + ddlSkill.SelectedValue + "'").FirstOrDefault();
        return scoreInfo != null ? scoreInfo.Score : "";

    }
   
}
